package org.piggy.core.filter;

import lombok.extern.slf4j.Slf4j;
import org.piggy.core.context.GatewayContext;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class GatewayFilterChain {

    private final List<Filter> filters;

    public GatewayFilterChain(){
        filters = new ArrayList<>();
    }

    public GatewayFilterChain addFilter(Filter filter){
        filters.add(filter);
        return this;
    }
    public GatewayFilterChain addFilterList(List<Filter> filter){
        filters.addAll(filter);
        return this;
    }

    public void doFilter(GatewayContext ctx) throws Exception {
        if(filters.isEmpty()){
            return;
        }
        for(Filter fl : filters){
            fl.doFilter(ctx);
            if (ctx.isTerminated()) {
                ctx.invokeTerminatedCallBack();
                break;
            }
        }
    }
}
